<template>
  <view class="loginbg">
    <view class="tips">为提供更好的使用体验，当前页面需要授权微信登录</view>
    <view class="denglu2"><button @tap="getuserinfo">点击授权登录</button></view>
  </view>
</template>


<script>
	import {mapMutations,mapGetters} from 'vuex';
export default {
  data() {
    return {
     
    };
  },
  mounted() {
     // this.login()
  },
  computed:{
	  ...mapGetters([
		  'getLocale'
	  ])
  },
  methods: {
	  ...mapMutations([
	  	   'login'
	  ]),
    getuserinfo() {
      const that = this;
      uni.login({
        success: function (res) {
          if (res.code) {
			  console.log("code: ",res.code);
            uni.request({
              url: 'https://boss.daxinxin.vip/boss/WxLogin',
              data: {
                code: res.code,
              },
              success: function (e) {
                if (e.data.code == 1) {
                  //_that.loadding(false);
                 
                  uni.hideLoading();
                } else {
                  uni.setStorageSync('token', e.data.data.token);
                  const session_id = e.data.data.openid;
                  // An highlighted block
                  wx.showModal({
                    title: '温馨提示',
                    content: '正在请求您的个人信息',
                    success(res) {
                      if (res.confirm) {
                        uni.showLoading({
                          title: '授权中',
                        });
                        wx.getUserProfile({
                          desc: '获取你的昵称、头像、地区及性别',
                          success:  (res) => {
                          console.log(res);
                          const { nickName, avatarUrl } = res.userInfo;
						  uni.setStorageSync("nickName",nickName )
						  uni.setStorageSync("avatarUrl",avatarUrl )
						  uni.showToast({
						      title: '授权成功',
						      icon: 'none',
						      duration: 2000,
						      success: function () {
								  that.login()
						        setTimeout(function() {
						          uni.navigateTo({
						            url: '/pages/index/index',
						          })
						        }, 2000);
						      }
						  });
					
						
                          },
                          fail: (res) => {
                            //拒绝授权
                            uni.hideLoading();
                            
						
                            return;
                          },
                        });
                      } else if (res.cancel) {
                        //拒绝授权 showErrorModal是自定义的提示
                       
                        return;
                      }
                    },
                  });

             
                }
              },
            });
          } else {
           uni.showToast({
           	title:"授权失败"
           								
           })
            uni.reLaunch({
              url: '/pages/index/index',
            });
          }
        },
      });
    },
  },
};
</script>

<style>
/* pages/index/login.wxss */

page {
  width: 100%;
  height: 100%;
  background-color: #f7f7f7;
}

.loginbg {
  width: 100%;
  height: 100vh;
  background-color: #f4f6f7;
  background-image: url('https://xxyg.daxinxin.vip/wxapp/loginbg.png');
  background-position: bottom center;
  background-size: 100%;
  background-repeat: no-repeat;
}
.denglu2 {
  width: 60%;
  justify-content: center;
  align-items: center;
  height: 60rpx;
  line-height: 60rpx;
  margin: 0 auto;
}
.denglu2 button {
  justify-content: center;
  align-items: center;
  height: 70rpx;
  line-height: 70rpx;
  margin: 0 auto;
  background-color: #46bdf6;
  color: #fff;
  border-radius: 35rpx;
  display: block;
}
.tips {
  color: rgb(182, 176, 176);
  width: 100%;
  text-align: center;
  font-size: 30rpx;
  height: 50rpx;
  line-height: 50rpx;
  padding-top: 66%;
  margin-bottom: 50rpx;
}
</style>